
package com.nec.recommend.module.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nec.recommend.exception.ServiceException;
import com.nec.recommend.module.system.entity.SysUser;
import com.nec.recommend.module.system.dto.UserInfoDTO;
import com.nec.recommend.module.system.mapper.UserMapper;
import com.nec.recommend.module.system.service.UserService;
import com.nec.recommend.utils.AuthUserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 用户表;(mn_user)表服务实现类
 *
 * @author : limingzhen
 * @date : 2022-12-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements UserService {


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysUser queryById(Long id) {
        SysUser sysUser = getBaseMapper().selectById(id);
        if (Objects.nonNull(sysUser)) {
            sysUser.setPassword(null);
        }
        return sysUser;
    }


    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Transactional(rollbackFor = Exception.class)
    public SysUser insert(SysUser sysUser) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getPhone, sysUser.getPhone());
        SysUser user = getBaseMapper().selectOne(wrapper);
        if (Objects.nonNull(user)) {
            throw new ServiceException("用户已存在！");
        }
        if (StringUtils.isNotEmpty(sysUser.getPassword())) {
            sysUser.setPassword(AuthUserUtils.encodePwd(sysUser.getPassword(), sysUser.getPhone()));
        } else {
            sysUser.setPassword(AuthUserUtils.encodePwd("123456", sysUser.getPhone()));
        }
        // 设置默认头像
        sysUser.setAvatar("/default_avatar.png");
        getBaseMapper().insert(sysUser);
        return sysUser;
    }

    /**
     * 更新数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(SysUser sysUser) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getPhone, sysUser.getPhone()).ne(SysUser::getId, sysUser.getId());
        SysUser user = getBaseMapper().selectOne(wrapper);
        if (Objects.nonNull(user)) {
            throw new ServiceException("用户已存在！");
        }
        if (StringUtils.isNotEmpty(sysUser.getPassword())) {
            sysUser.setPassword(AuthUserUtils.encodePwd(sysUser.getPassword(), sysUser.getPhone()));
        } else if (StringUtils.equals(sysUser.getPassword(), "")) {
            sysUser.setPassword(null);
        }
        return getBaseMapper().updateById(sysUser) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Long id) {
        int total = getBaseMapper().deleteById(id);
        return total > 0;
    }

    @Override
    public UserInfoDTO queryByAccount(String account) {
        return getBaseMapper().getUserInfo(account);
    }

    @Override
    public boolean updatePwd(String account, String oldPwd, String newPwd) {
        LambdaUpdateWrapper<SysUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SysUser::getPhone, account).eq(SysUser::getPassword, oldPwd);
        SysUser sysUser = new SysUser();
        sysUser.setPassword(newPwd);
        return getBaseMapper().update(sysUser, wrapper) > 0;
    }

}